var t = getApp();

Page({
    data: {
        active: "0",
        status: !1,
        second_catgory: [],
        parentid: "0",
        childid: "",
        search_title: "",
        language: "",
        show1: !1,
        posterShow: !1,
        list: [],
        posterImg: "",
        isLoadMore: !0,
        page: 1,
        total_page: 0,
        timestart: 0,
        timeend: 0,
        kFootshow: !0,
        iPnoneBottomBol: !1,
        posterId: 0
    },
    onLoad: function(t) {
        this.getiPnoneBottom(), this.initData(), this.getData(), this.getCatgory();
    },
    onPageScroll: function() {
        this.setData({
            status: !1
        });
    },
    getiPnoneBottom: function() {
        try {
            wx.getStorageSync("iPhoneBottom") && this.setData({
                iPnoneBottomBol: !0
            });
        } catch (t) {}
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.data.isLoadMore ? this.getMoreData() : console.log("没有更多数据");
    },
    onShareAppMessage: function() {},
    initData: function() {
        this.setData({
            show1: !1,
            posterShow: !1,
            list: [],
            posterImg: "",
            isLoadMore: !0,
            page: 1,
            total_page: 0
        });
    },
    getData: function() {
        var a = this, e = t.getNetAddresss("plugin.material-center.api.centerlist.index");
        a.setData({
            isLoadMore: !1,
            page: 1
        });
        var o = {
            page: this.data.page,
            parentid: this.data.parentid,
            childid: this.data.childid,
            search_title: this.data.search_title
        };
        t._postNetWork({
            url: e,
            data: o,
            success: function(t) {
                var e = t.data;
                1 == e.result ? (a.setData({
                    isLoadMore: !0,
                    total_page: e.data.list.last_page
                }), a.setData({
                    list: e.data.list.data
                })) : wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    changeSecond: function(t) {
        var a = t.currentTarget.dataset.id;
        this.setData({
            childid: a
        }), this.getData();
    },
    close: function() {
        this.setData({
            status: !1
        });
    },
    onClick: function(t) {
        var a = this;
        this.setData({
            parentid: t.detail.name,
            childid: ""
        }), this.getData(), "0" == this.data.parentid ? this.setData({
            second_catgory: [],
            status: !1
        }) : this.data.catgoryList.forEach(function(t, e) {
            t.id == a.data.parentid && a.setData({
                second_catgory: t.childrens,
                status: !0
            });
        });
    },
    getMoreData: function() {
        var a = this, e = t.getNetAddresss("plugin.material-center.api.centerlist.index");
        a.setData({
            isLoadMore: !1
        }), a.data.page >= a.data.total_page || (a.setData({
            page: a.data.page + 1
        }), t._getNetWork({
            url: e,
            data: {
                page: a.data.page,
                parentid: a.data.parentid,
                childid: a.data.childid,
                search_title: a.data.search_title
            },
            success: function(t) {
                var e = t.data;
                if (1 == e.result) {
                    var o = e.data.list.data;
                    a.setData({
                        isLoadMore: !0,
                        list: a.data.list.concat(o)
                    });
                } else a.setData({
                    page: a.data.page - 1,
                    isLoadMore: !1
                });
            },
            fail: function(t) {
                console.log(t);
            }
        }));
    },
    showimage: function(t) {
        var a = t.currentTarget.dataset.images, e = t.currentTarget.dataset.imgidx;
        wx.previewImage({
            current: a[e] ? a[e] : "",
            urls: a || []
        });
    },
    toSousuo: function() {
        wx.navigateTo({
            url: "/packageA/others/materialSearch/materialSearch"
        });
    },
    toGoods: function(t) {
        var a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + a
        });
    },
    update: function(a) {
        var e = a.currentTarget.dataset.id, o = a.currentTarget.dataset.title;
        wx.setClipboardData({
            data: o || "",
            success: function(t) {
                wx.showToast({
                    title: "复制成功",
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function() {
                wx.showToast({
                    title: "复制失败",
                    icon: "none",
                    duration: 1500
                });
            }
        }), this.setData({
            show1: !0
        });
        var s = t.getNetAddresss("plugin.material-center.api.centerlist.updateNum");
        t._getNetWork({
            url: s,
            data: {
                id: e,
                params: "share"
            },
            success: function(t) {},
            fail: function(t) {
                console.log(t);
            }
        });
    },
    show1Close: function() {
        this.setData({
            show1: !1
        });
    },
    getCatgory: function() {
        var a = this, e = t.getNetAddresss("plugin.material-center.api.centerlist.getCategoryList");
        t._getNetWork({
            url: e,
            data: {},
            success: function(t) {
                var e = t.data;
                1 == e.result ? (a.setData({
                    catgoryList: e.data
                }), console.log(a.data.catgoryList)) : wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    download: function(t) {
        var a = t.currentTarget.dataset.images || [], e = t.currentTarget.dataset.video || [];
        a.length > 0 && (wx.showLoading({
            title: "正在下载图片..."
        }), this.loadImg(a)), e && e.video_url && (wx.showLoading({
            title: "正在下载视频..."
        }), this.loadVideo(e));
    },
    loadImg: function(t) {
        t.forEach(function(t) {
            wx.downloadFile({
                url: t,
                success: function(t) {
                    wx.saveImageToPhotosAlbum({
                        filePath: t.tempFilePath,
                        success: function(t) {
                            wx.hideLoading(), wx.showToast({
                                title: "图片下载成功",
                                icon: "success",
                                duration: 2e3
                            });
                        },
                        fail: function(t) {
                            wx.hideLoading(), "saveImageToPhotosAlbum:fail auth deny" === t.errMsg && wx.openSetting({
                                success: function(t) {
                                    console.log(t), t.authSetting["scope.writePhotosAlbum"] ? console.log("获取权限成功，给出再次点击图片保存到相册的提示。") : console.log("获取权限失败，给出不给权限就无法正常使用的提示");
                                }
                            });
                        }
                    });
                }
            });
        });
    },
    loadVideo: function(t) {
        wx.downloadFile({
            url: t.video_url,
            success: function(t) {
                wx.saveVideoToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: "视频下载成功",
                            icon: "success",
                            duration: 2e3
                        });
                    },
                    fail: function(t) {
                        wx.hideLoading(), "saveVideoToPhotosAlbum:fail auth deny" === t.errMsg && wx.openSetting({
                            success: function(t) {
                                console.log(t), t.authSetting["scope.saveVideoToPhotosAlbum"] ? console.log("获取权限成功，给出再次点击视频保存到相册的提示。") : console.log("获取权限失败，给出不给权限就无法正常使用的提示");
                            }
                        });
                    }
                });
            }
        });
    },
    postShow: function(a) {
        var e = this, o = a.currentTarget.dataset.id;
        if (o) if (e.data.posterImg && this.data.posterId == o) this.setData({
            posterShow: !0
        }); else {
            this.data.posterId = o, wx.showLoading({
                title: "海报生成中"
            });
            var s = t.getNetAddresss("goods.goods-poster.generate-goods-poster");
            t._getNetWork({
                url: s,
                data: {
                    id: o
                },
                success: function(t) {
                    var a = t.data;
                    1 == a.result ? e.setData({
                        posterShow: !0,
                        posterImg: a.data.image_url
                    }) : (e.setData({
                        posterShow: !1
                    }), wx.showToast({
                        title: a.msg,
                        icon: "none",
                        duration: 1500
                    })), wx.hideLoading();
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        } else wx.showToast({
            title: "该素材未设置关联商品",
            icon: "none",
            duration: 1500
        });
    },
    posterShowClose: function() {
        this.setData({
            posterShow: !1
        });
    },
    timestart: function(t) {
        this.setData({
            timestart: t.timeStamp
        });
    },
    timeend: function(t) {
        this.setData({
            timeend: t.timeStamp
        });
    },
    saveImg: function() {
        wx.getSetting({
            success: function(t) {
                t.authSetting["scope.writePhotosAlbum"] || wx.authorize({
                    scope: "scope.writePhotosAlbum",
                    success: function() {
                        console.log("授权成功");
                    }
                });
            }
        });
        var t = this.data.posterImg;
        wx.downloadFile({
            url: t,
            success: function(t) {
                console.log(t), wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        wx.showToast({
                            title: "保存成功",
                            icon: "none",
                            duration: 2e3
                        });
                    },
                    fail: function(t) {
                        console.log(t), "saveImageToPhotosAlbum:fail auth deny" === t.errMsg && (console.log("当初用户拒绝，再次发起授权"), 
                        wx.openSetting({
                            success: function(t) {
                                t.authSetting["scope.writePhotosAlbum"] ? wx.showToast({
                                    icon: "none",
                                    title: "获取权限成功,再次长按保存图片",
                                    duration: 1500
                                }) : wx.showToast({
                                    icon: "none",
                                    title: "获取权限失败,保存图片需要用户授权",
                                    duration: 1500
                                });
                            }
                        }));
                    },
                    complete: function(t) {
                        console.log(t);
                    }
                });
            }
        });
    }
});